
#include <mjin2/mjin2.h>
#include <iostream>
#include <time.h>

void printXMLTree(mjin2::XMLNode &n, mjin2::u8 level = 0)
{
    for (mjin2::u8 i = 0; i < level; ++i)
        std::cout << " ";
    std::cout << n.name << " " << n.value << std::endl;
    mjin2::XMLAttributes::iterator at = n.attrs.begin();
    for (; at != n.attrs.end(); ++at)
    {
        for (mjin2::u8 i = 0; i < level + 2; ++i)
            std::cout << " ";
        std::cout << at->name << " = " << at->value << std::endl;
    }
    mjin2::XMLNodes::iterator it = n.nodes.begin();
    for (; it != n.nodes.end(); ++it)
        printXMLTree(*it, level + 1);
}

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        std::cout << MJIN2_STR("Usage: %s /path/to/t/mjin2\n", argv[0]);
        return 0;
    }
    mjin2::String dataDir = argv[1];
    dataDir += "/Renderless";
    mjin2::Application app("Renderless", "log.html");
    std::cout << mjin2::buildInfo() << std::endl;
    std::cout << mjin2::version()   << std::endl;
    mjin2::RegExp re("resolve.conf$|host.conf$");
    std::cout << re.match("/etc/resolve.conf") << std::endl;
    std::cout << re.match("/etc/host.conf") << std::endl;
    std::cout << re.match("/etc/asound.conf") << std::endl;
    re.setPattern("/");
    std::cout << "/: " << re.match("/etc/asound.conf") << std::endl;
    std::cout << mjin2::strReplace("A dog \nand a cat", "a cat|\n|A", "a dog") << std::endl;
    mjin2::srand(time(0));
    std::cout << mjin2::rand() << std::endl;
    mjin2::mkdir("./abc/def/ghi");
    std::cout << mjin2::distribution() << std::endl;
    mjin2::PathResolver *resolver = app.pathResolver();
    resolver->addSearchPath(dataDir);
    mjin2::ConfigFile cfg(resolver->findFile("os-release"));
    std::cout << cfg.str();
    app.logger()->log("Logging from C++");
    mjin2::String xmlFileName = resolver->findFile("mj.xml");
    mjin2::StringList searchPaths = resolver->searchPaths();
    for (auto it = searchPaths.begin(); it != searchPaths.end(); ++it)
        std::cout << "search path: " << *it << std::endl;
    mjin2::XML xml;
    mjin2::XMLNode node = xml.load(xmlFileName);
    printXMLTree(node);
    mjin2::InputTranslator itr;
    std::cout << itr.string(mjin2::INPUT_KEY_X) << " = " << itr.id("INPUT_KEY_X") << std::endl;

    return 0;
}

